package cn.sdormitory.violative.form;

import cn.hutool.core.date.DatePattern;
import cn.sdormitory.common.PageForm;
import cn.sdormitory.violative.entity.ViolationPatrol;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springframework.format.annotation.DateTimeFormat;

import java.util.Calendar;
import java.util.Date;

@EqualsAndHashCode(callSuper = false)
@Data
public class ViolationPatrolQueryForm extends PageForm<ViolationPatrolQueryForm,ViolationPatrol> {
    @ApiModelProperty("违纪编号")
    private Long id;

    @ApiModelProperty(value = "学号")
    private String studentNo;

    @ApiModelProperty(value = "班级编号")
    private String classId;

    @ApiModelProperty(value = "接受班主任编号")
    private Long receiverClassTeacherId;

    @ApiModelProperty(value = "违纪日期")
    @JsonFormat(pattern = DatePattern.NORM_DATE_PATTERN, timezone = "GMT+8")
    @DateTimeFormat(pattern = DatePattern.NORM_DATE_PATTERN)
    private Date violationDay;

    private boolean queryImages = false;

    @ApiModelProperty("大于该状态")
    private Integer gtStatus;

    @Override
    public QueryWrapper<ViolationPatrol> query() {
        QueryWrapper<ViolationPatrol> query = super.query();
        if(violationDay != null){
            Date startDate = violationDay;
            String violationTimeColumnName = getColumn(ViolationPatrol::getViolationTime);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(startDate);
            calendar.add(Calendar.DATE,1);
            Date endDate = calendar.getTime();
            query.between(violationTimeColumnName,violationDay,endDate);
        }
        if(!queryImages){
            String imagesColumn = getColumn(ViolationPatrol::getImages);
            query.select(info->!imagesColumn.equals(info.getColumn()));
        }
        query.gt(gtStatus != null, getColumn(ViolationPatrol::getStatus), gtStatus);
        return query;
    }
}
